<?php
namespace app\hotback\controller;
use think\Db;
class Technlevel extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 技师等级列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [
					'is_delete_time' => 0
				];
				if(!empty($params['title']))
				{
					$map['title'] = ['like','%'.$params['title'].'%'];
				}
				$count = Db::name('techn_level')->where($map)->count();
				
				$map['proxy_id']=0;
				
				$list_data = Db::name('techn_level')->where($map)->order('rank desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//编辑url
						$list_data[$key]['save_url'] = url('Technlevel/saveinfo', ['id' => $value['id']]);
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 技师等级添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		if(!empty($params['id']))
		{
			//查询
			$info = Db::name('techn_level')->where(['id' => $params['id']])->find();
		}
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 技师等级添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$validate = $this->validate(
				$params,
				[
					'title' => 'require|unique:techn_level,is_delete_time=0&title='.$params['title'],
				// 	'rank' => 'require|unique:techn_level,is_delete_time=0&rank='.$params['rank'].'|integer',
					'divide_into' => 'require|integer',
					'low_result' => ['regex' => '^([0-9]{1}\d{0,6})(\.\d{1,2})?$'],
					'online_duration' => ['regex' => '^([0-9]{1}\d{0,6})(\.\d{1,2})?$'],
					'plus_clock_rate' => 'integer',
					'integral' => 'integer'
				],
				[
					'title.require' => '请输入等级名称',
					'title.unique' => '等级名称已存在',
				// 	'rank.require' => '请输入对应等级',
				// 	'rank.unique' => '对应等级已存在',
				// 	'rank.integer' => '对应等级只能是整数',
					'divide_into.require' => '请输入分成比例',
					'divide_into.integer' => '分成比例只能为整数',
					'low_result.regex' => '最低业绩格式有误',
					'online_duration.regex' => '在线时长格式有误',
					'plus_clock_rate.integer' => '加钟率只能为整数',
					'integral.integer' => '积分只能为整数'
				]
			);
			if(true !== $validate)
			{
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
// 			echo 1;exit;
			//基础数据
			$data = [
				'title' => $params['title'],
				'rank' => $params['rank'],
				'divide_into' => $params['divide_into'],
				'low_result' => isset($params['low_result']) ? $params['low_result'] : 0,
				'online_duration' => isset($params['online_duration']) ? $params['online_duration'] : 0,
				'plus_clock_rate' => isset($params['plus_clock_rate']) ? $params['plus_clock_rate'] : 0,
				'integral' => isset($params['integral']) ? $params['integral'] : 0,
				'jz_bl' => $params['jz_bl'],
			];
			//算 加钟金额=最低业绩*加钟率
			$data['plus_clock_price'] = PriceNumberFormat($data['low_result']*($data['plus_clock_rate']/100));
			// 添加/编辑
			if(empty($params['id']))
			{
				$data['add_time'] = time();
        		$data['upd_time'] = time();
        		$techn_level_id = Db::name('techn_level')->insertGetId($data);
			}else
			{
				$data['upd_time'] = time();
				if(Db::name('techn_level')->where(['id'=>intval($params['id'])])->update($data))
				{
					$techn_level_id = $params['id'];
				}
			}
			//是否成功
			if(isset($techn_level_id) && $techn_level_id > 0)
			{
				$result['code'] = 0;
				$result['msg'] = "操作成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "操作失败";
			}
		}
		echo json_encode($result);
	}

	/**
	 * 技师等级删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$data = [
				'is_delete_time' => time(),
				'upd_time' => time()
			];
			if(Db::name('techn_level')->where(['id' => intval($params['id'])])->update($data))
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
	
	public function zhouqi(){
	    $result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			if(Db::name('xunh')->where(['proxy_id' => 0])->update(['day'=>$params['day']]))
			{
				$result['code'] = 0;
				$result['msg'] = "成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "失败";
			}
		}
		echo json_encode($result);
	}
}